package com.youhome.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.BShareLogMapper;
import com.youhome.service.model.BShareLog;
import com.youhome.service.model.DateItem;
import com.youhome.service.service.BShareLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2017/11/3.
 */
@Service
public class BShareLogServiceImpl implements BShareLogService {
    @Autowired
    private BShareLogMapper bShareLogMapper;

    @Override
    public int create(BShareLog log) {
        log.setCtime(new Date());
        return bShareLogMapper.insertSelective(log);
    }

    @Override
    public PageInfo<BShareLog> selectByShareId(Integer pageNumber, Integer pageSize, String shareId) throws CustomException {
        if (StringUtils.isEmpty(shareId)) {
            throw  new CustomException(ResultCode.ERROR_PARAMETER);
        }
        if (pageNumber == null || pageNumber < 1) {
            pageNumber = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);

        List<BShareLog> list = bShareLogMapper.selectByShareId(shareId);

        return new PageInfo<>(list);
    }

    @Override
    public List<DateItem> selectLast30DayDateItemByShareId(String shareId) throws CustomException {
        if (StringUtils.isEmpty(shareId)) {
            throw  new CustomException(ResultCode.ERROR_PARAMETER);
        }
        return bShareLogMapper.selectLast30DayDateItemByShareId(shareId);
    }
}
